/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.freedom.imarket.controller.json.designer;

import org.freedom.util.HibernateUtils;
import org.hibernate.mapping.Property;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author edoardo
 */
public class FormDesigner {

    public static JSONArray create(Class entityClass) throws JSONException {

        Property[] properties = HibernateUtils.getEntityProperties(entityClass);
        JSONArray arr = new JSONArray();
        for (int i = 0; i < properties.length; i++) {
            Property property = properties[i];
            if (!property.isComposite()) {
                String fieldType = property.getType().getName();
                Xtype xtype = DataTypeUtil.getExtTypeFromHibernateType(fieldType);
                System.out.println("Type: " + xtype + ", property: " + property.getName() + ", fieldtype: " + property.getType());
                JSONObject obj = new JSONObject();
                obj.put("xtype", xtype.toString());
                obj.put("name", property.getName());
                obj.put("fieldLabel", property.getName());
                arr.put(obj);
            }
        }
        return arr;


    }

    public static String designButtons(Class entityClass) throws JSONException {

        // qui da mettere il riferimento a qualche file di configurazione per l'entity class. 
        JSONObject saveButton = new JSONObject();
        saveButton.put("text", "Save");
        saveButton.put("action", "save");
        JSONObject cancelButton = new JSONObject();

        StringBuilder sb = new StringBuilder();

        sb.append("[{text: 'Save', action: 'save' },{text: 'Cancel', scope: this, handler: this.close}]");

        return sb.toString();
    }
}
